package com.example.Conf;

import com.example.Utils.UserLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserLoginInterceptor(stringRedisTemplate))
                //配置放行路径
                .excludePathPatterns(
                        "/user/login",
                        "/swagger**/**",
                        "/admin/**",
                        "/menu/**"
                );
        // 添加 AdminInterceptor
        // registry.addInterceptor(new AdminLoginInterceptor(stringRedisTemplate))
        //         .excludePathPatterns(
        //                 "/admin/login",
        //                 "/swagger**/**"
        //         )
        //         .addPathPatterns("/admin/**"); // 只拦截 /admin/ 目录下的路径
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

